package net.j4love.nio.charset;

import org.junit.Test;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

/**
 *
 * @author he peng
 * @create 2018/3/20 13:59
 * @see
 */
public class CharsetTest {

    // 16位Unicode code units和字节序列之间的命名映射

    // 构造从规范字符集名称到字符集对象的排序映射
    @Test
    public void availableCharsets() throws Exception {
        SortedMap<String, Charset> charsetSortedMap = Charset.availableCharsets();
        for (Map.Entry<String, Charset> entry : charsetSortedMap.entrySet()) {
            System.out.println("key = " + entry.getKey() + " , value = " + entry.getValue());
        }
    }

    // 返回此Java虚拟机的默认字符集
    @Test
    public void defaultCharset() throws Exception {
        Charset charset = Charset.defaultCharset();
        System.out.println(charset);
    }

    @Test
    public void forName() throws Exception {
        Charset charset = Charset.forName("UTF-8");
        System.out.println(charset);
    }

    @Test
    public void isSupported() throws Exception {
        boolean supported = Charset.isSupported("UTF-8");
        System.out.println(supported);
    }

    @Test
    public void aliases() throws Exception {
        Charset charset = Charset.forName("unicode-1-1-utf-8");
        Set<String> aliases = charset.aliases();
        System.out.println(aliases);
    }

    @Test
    public void canEncode() throws Exception {
        Charset charset = Charset.forName("utf-8");
        boolean canEncode = charset.canEncode();
        System.out.println(canEncode);
    }

    @Test
    public void displayName() throws Exception {
        Charset charset = Charset.forName("utf-8");
        System.out.println(charset.displayName());
        System.out.println(charset.displayName(Locale.SIMPLIFIED_CHINESE));
    }

    @Test
    public void encode() throws Exception {
        Charset charset = Charset.forName("utf-8");
        ByteBuffer byteBuffer = charset.encode("hello world!");
        System.out.println(byteBuffer);
    }

    @Test
    public void newEncoder() throws Exception {
        Charset charset = Charset.forName("utf-8");
        CharsetEncoder charsetEncoder = charset.newEncoder();
    }
}
